import re

"""
(\S+) 或 (.*?) 捕获为一个组
(\S+) (\S+) 捕获为2个组
"""
text = "[test.png](http://www.qq.com/test.png)"


def test1():
    """匹配 方括号[xxx] 中的数据
    [test.png] --> test.png
    """
    pattern = r"\[\S+\]"  # '[test.png]'
    pattern = r"\[(\S+)\]"  # 'test.png'
    pattern = r"\[(.*?)\]"  # 'test.png'
    match = re.search(pattern, text)
    print(match.group(0))
    print(match.group(1))


def test2():
    """匹配 括号(xxx) 中的数据
    (http://www.qq.com/test.png) --> http://www.qq.com/test.png
    """
    pattern = r"\((\S+)\)"  # http://www.qq.com/test.png
    pattern = r"\((.*?)\)"  # http://www.qq.com/test.png
    match = re.search(pattern, text)
    print(match.group(0))
    print(match.group(1))


def test3():
    """匹配 括号[xxx](yyy) 中的数据
    [test.png](http://www.qq.com/test.png) -->
    test.png
    http://www.qq.com/test.png
    """
    # pattern = r"\[(\S+)\]\((\S+)\)" # --OK
    pattern = r"\[(.*?)\]\((.*?)\)"  # --OK
    match = re.search(pattern, text)
    print(match.group(1))
    print(match.group(2))


"匹配 [xxx]"
test1()
"匹配 (xxx)"
test2()
"匹配 [xxx](xxx)"
test3()
